<?php

namespace App\Admin\Controllers;

use Encore\Admin\Http\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Table;
use Encore\Admin\Show;
use App\Models\LinkModel;

class LinkController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '友情链接';

    /**
     * Make a table builder.
     *
     * @return Table
     */
    protected function table()
    {
        $table = new Table(new LinkModel);

        $table->filter(function($filter){
            $filter->like('name', '名称');
            $filter->equal('status', '状态')->select([
                LinkModel::STATUS_DISABLE => '禁用',
                LinkModel::STATUS_ENABLE => '启用',
            ]);
        });

        $table->column('id', __('ID'))->sortable();
        $table->column('created_at', '创建时间');
        $table->column('updated_at', '修改时间');
        $table->column('name', '名称');
        $table->column('url', '链接')->link();
        $table->column('status', '状态')->using([
            LinkModel::STATUS_DISABLE => '禁用',
            LinkModel::STATUS_ENABLE => '启用',
        ]);
        $table->column('sort', '排序');

        return $table;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(LinkModel::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('created_at', '创建时间');
        $show->field('updated_at', '修改时间');
        $show->field('name', '名称');
        $show->field('url', '链接');
        $show->field('status', '状态')->using([
            LinkModel::STATUS_DISABLE => '禁用',
            LinkModel::STATUS_ENABLE => '启用',
        ]);
        $show->field('sort', '排序');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new LinkModel);

        $form->display('id', __('ID'));
        $form->display('created_at', '创建时间');
        $form->display('updated_at', '修改时间');
        $form->text('name', '名称')->rules('required');
        $form->text('url', '链接')->rules('required');
        $form->select('status', '状态')->options([
            LinkModel::STATUS_DISABLE => '禁用',
            LinkModel::STATUS_ENABLE => '启用',
        ])->rules('required')->default(LinkModel::STATUS_ENABLE);
        $form->number('sort', '排序')->default(1);

        return $form;
    }
}
